package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.sense.kukoo.common.R;

import java.util.ArrayList;
import java.util.List;

public class SubscriptionsChannelView extends FrameLayout {

    private View tv_see_all;
    private ArrayList<SubscriptionsChannelItemView> defItemViews;
    private ArrayList<SubscriptionsChannelItemView> currentItemViews;
    private int mSize;

    public SubscriptionsChannelView(Context context) {
        super(context);
        initView();
    }

    public SubscriptionsChannelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SubscriptionsChannelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public SubscriptionsChannelView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.common_subscriptions_channel, this, true);
        //
        tv_see_all = findViewById(R.id.tv_see_all);
        currentItemViews = new ArrayList<>(8);
        defItemViews = new ArrayList<>(8);
        defItemViews.add(findViewById(R.id.item_1));
        defItemViews.add(findViewById(R.id.item_2));
        defItemViews.add(findViewById(R.id.item_3));
        defItemViews.add(findViewById(R.id.item_4));
        defItemViews.add(findViewById(R.id.item_5));
        defItemViews.add(findViewById(R.id.item_6));
        defItemViews.add(findViewById(R.id.item_7));
        defItemViews.add(findViewById(R.id.item_8));
        //
        tv_see_all.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onSeeAll();
                }
            }
        });
        for (SubscriptionsChannelItemView itemView : defItemViews) {
            itemView.setItemClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //自动清除红点
                    //itemView.setRedDot(false);
                    //
                    int id = itemView.getId();
                    for (int i = 0; i < currentItemViews.size(); i++) {
                        if (currentItemViews.get(i).getId() == id) {
                            if (mListener != null){
                                mListener.onChannelItemClick(i);
                            }
                        }
                    }
                }
            });
        }
    }

    public void setSize(int size){
        this.mSize = size;
        currentItemViews.clear();
        if(size <= 1 || size >= 8){
            for (int i = 0; i < 8; i++) {
                SubscriptionsChannelItemView view = defItemViews.get(i);
                if(i < size){
                    view.setVisibility(View.VISIBLE);
                    currentItemViews.add(view);
                }else{
                    view.setVisibility(View.GONE);
                }
            }
        }else{
            int left = size - 1;
            for (int i = 0; i < 8; i++) {
                SubscriptionsChannelItemView view = defItemViews.get(i);
                if(i < left){
                    view.setVisibility(View.VISIBLE);
                    currentItemViews.add(view);
                }else{
                    view.setVisibility(View.GONE);
                }
            }
            SubscriptionsChannelItemView right = defItemViews.get(7);
            right.setVisibility(View.VISIBLE);
            currentItemViews.add(right);
        }
    }

    public List<SubscriptionsChannelItemView> getItems(){
        return currentItemViews;
    }

    private OnChannelItemEventListener mListener;

    public interface OnChannelItemEventListener{
        void onSeeAll();
        void onChannelItemClick(int position);
    }

    public void setListener(OnChannelItemEventListener l){
        mListener = l;
    }
}
